<template>
  <el-dialog :title="'确认'+name" :visible.sync="show" width="35%" @close="close">
    确定{{name}}该条记录吗？
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button @click="ensure">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
    export default {
        name:'searchPopUp',
        props:{
            outBtn:Object,
            deleteUrl:String,
            disableUrl:String,
            enableUrl:String,
            originalData:Object,
            queryProps:Array
            // prop:String,
            // queryProp:String
        },
        data() {
            return{
                show:false,
                title:'',
                name:''
            }
        },
        methods:{
            close() {
                this.$emit("close",null)
            },
            ensure() {
                if (!this.originalData) {
                    this.$message.error("请选择一条数据")
                }
                let url = "";
                if (this.deleteUrl==this.$api.API_SET_CUSTOMER_GROUP || this.deleteUrl == this.$api.API_SET_RETAIL_TYPE || this.deleteUrl == this.$api.API_SET_RETAIL_LEVEL || this.deleteUrl == this.$api.API_SET_RETAIL_TYPE || this.deleteUrl == this.$api.API_SET_MANAGEMENT_HIERARCHY || this.deleteUrl == this.$api.API_SETZONESTATE || this.deleteUrl == this.$api.API_SETREGIONSTATE ||  this.deleteUrl == this.$api.API_SETCITYSTATE ||  this.deleteUrl == this.$api.API_ADMIN_CITY_SET ||  this.deleteUrl == this.$api.API_SETPROVINCESTATE||  this.deleteUrl == this.$api.API_SET_LOCATION || this.deleteUrl == this.$api.API_SET_ATTR || this.deleteUrl == this.$api.API_SET_DEP || this.deleteUrl == this.$api.API_SET_REG || this.deleteUrl == this.$api.API_SET_CHANNEL){
                    switch (this.outBtn) {
                        case this.$config.BTNS.DELETE:
                            this.originalData.isDelete = 1
                            break;
                        case this.$config.BTNS.ENABLE:
                            this.originalData.isValid = 1
                            break;
                        case this.$config.BTNS.FORBIDEN:
                            this.originalData.isValid = 0
                            break;
                    }
                    this.$http.post(this.deleteUrl,this.originalData).then(resp=>{
                        this.$message.success(resp||"操作成功")
                        this.close()
                        this.refresh()
                    },msg=>{})
                }else {
                    switch (this.outBtn) {
                        case this.$config.BTNS.DELETE:
                            url = this.deleteUrl
                            break;
                        case this.$config.BTNS.ENABLE:
                            url = this.enableUrl
                            break;
                        case this.$config.BTNS.FORBIDEN:
                            url = this.disableUrl
                            break;
                    }
                    if (this.queryProps) {
                        for (let i=0;i<this.queryProps.length;i++) {
                            this.originalData[this.queryProps[i].queryProp] = this.originalData[this.queryProps[i].prop]
                        }
                    }
                    this.$http.post(url,this.originalData).then(resp=>{
                        this.$message.success(resp||"操作成功")
                        this.close()
                        this.refresh()
                    },msg=>{})
                }

                this.close()
            },
            refresh() {
                this.$emit("refresh")
            }
        },
        watch:{
            outBtn(newVal,oldVal) {
                if (newVal) {
                    this.name = newVal.name
                }
                this.show = (newVal==this.$config.BTNS.DELETE || newVal==this.$config.BTNS.FORBIDEN || newVal==this.$config.BTNS.ENABLE)
            },
        }
    }
</script>
